﻿using System;
using System.Reactive.Concurrency;
using System.Threading;
using System.Windows.Threading;
using NUnit.Framework;
using Present.Commands.Decorators;
using Present.Commands.Rx;

namespace Present.Commands.Tests
{
    public class TestCase
    {
        [SetUp]
        public virtual void Setup()
        {
            BackgroundWorkerFactory.Create = () => new SynchronousBackgroundWorker();
            SchedulerFactory.UiSchedulerCreator = () => Scheduler.NewThread;
        }

        protected void EnableAsyncCalls()
        {
            BackgroundWorkerFactory.Create = () => new BackgroundWorkerDecorator();
        }

        /// <summary>
        /// BEcause CommandManager uses the dispatcher, we need to proccess the messages it sends.
        /// </summary>
        public static void DoEvents()
        {
            var f = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(
                DispatcherPriority.Background,
                (SendOrPostCallback)delegate(object arg)
                {
                    var fr = arg as DispatcherFrame;
                    fr.Continue = false;
                },
                f);
            Dispatcher.PushFrame(f);
        }

        protected void RunOnBackgroundThread(Action run)
        {
            new ThreadStart(run).Invoke();
        }

    }
}
